﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Todo.Entity.Models;
using Todo.Logic.Server;
using System.Linq;

namespace ToDoListTest
{
    class Program
    {
        private TodoContext _ctx = new TodoContext();
        UserServer _userServer = new UserServer();
        InventoryServer _inventoryServer = new InventoryServer();
        ThingServer _thingServer = new ThingServer();

        /// <summary>
        /// 测试
        /// </summary>
        public void Test()
        {
            Users user = new Users();
            Inventory inventory = new Inventory();
            Things thing = new Things();
            while (true)
            {
                Console.WriteLine("1.注册");
                Console.WriteLine("2.登陆");
                Console.WriteLine("3.退出");
                Console.WriteLine("请输入选择");
                int choice1 = InputANumber();
                switch (choice1)
                {
                    case 1: Register(); break;
                    case 2: user = Login();
                        UserMenu(user);break;
                    default: break;
                }
                if (choice1 == 3)
                    break;
            }
        }

        public void UserMenu(Users user)
        {
            Inventory inventory = new Inventory();

           if (user != null)
            {
                while (true)
                {
                    Console.WriteLine("1.查看所有清单");
                    Console.WriteLine("2.查看所有事项");
                    Console.WriteLine("3.统计完未完成事项");
                    Console.WriteLine("4.查看所有过期事项");
                    Console.WriteLine("5.创建一个清单");
                    Console.WriteLine("6.修改清单名称");
                    Console.WriteLine("7.查看清单所有事项");
                    Console.WriteLine("8.查看清单");
                    Console.WriteLine("9.进入事项菜单");
                    Console.WriteLine("10.删除清单");
                    Console.WriteLine("11.退出");
                    Console.WriteLine("请输入选择");
                    int choice2 = InputANumber();
                    switch (choice2)
                    {
                        case 1:
                            ViewAllList(user.Id); break;
                        case 2:
                            ViewAllThing(user.Id); break;
                        case 3:
                            CountOfNotCompelet(user.Id); break;
                        case 4:
                            OverTimeThing(user.Id); break;
                        case 5:
                            CreateInventory(user.Id); break;
                        case 6:
                            ChangeListName(user.Id); break;
                        case 7:
                            ViewAllThingInList(user.Id); break;
                        case 8:
                            ViewInventory(user.Id); break;
                        case 9:
                            InventoryMenu(user.Id);
                            break;
                        case 10:
                            DeleteInventory(user.Id);break;
                        default: break;
                    }
                    if (choice2 == 11)
                        break;
                }
            }
        }

        public void InventoryMenu(int userId)
        {
                while (true)
                {
                    Console.WriteLine("1.添加事项");
                    Console.WriteLine("2.删除事项");
                    Console.WriteLine("3.修改备注");
                    Console.WriteLine("4.修改过期时间");
                    Console.WriteLine("5.改变事件状态");
                    Console.WriteLine("6.查看事项");
                    Console.WriteLine("7.退出");
                    Console.WriteLine("请输入选择");
                    int choice3 = InputANumber();
                    switch (choice3)
                    {
                        case 1:
                            AddThing(userId); break;
                        case 2:
                            RemoveThing(userId); break;
                        case 3:
                            AddRemark(userId); break;
                        case 4:
                            AddExpirationDate(userId); break;
                        case 5:
                            ChangeStatue(userId); break;
                        case 6:
                            FindThing(userId); break;
                        default: break;
                    }
                    if (choice3 == 7)
                        break;
                }
        }

        /// <summary>
        /// 注册
        /// </summary>
        public void Register()
        {
            string nickname, password;
            Console.WriteLine("输入昵称");
            nickname = Console.ReadLine();

            if (_userServer.JudgeUserNameRepeat(nickname)) {
                Console.WriteLine("输入密码");
                password = Console.ReadLine();
                _userServer.Register(nickname, password);
                var userList = _ctx.Users.ToList();
                var user = _ctx.Users.SingleOrDefault(p=>p.UserName==nickname&&p.Password==password);
                Console.WriteLine("注册成功");
            }
            else
            {
                Console.WriteLine("注册失败，该昵称已存在");
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns>用户</returns>
        public Users Login()
        {
            Console.WriteLine("输入用户名");
            string name = Console.ReadLine();

            if (!_userServer.JudgeUserNameRepeat(name))
            {
                Console.WriteLine("输入密码");
                string password = Console.ReadLine();
                Users user = _userServer.Login(name, password);
                if (user != null)
                {
                    Console.WriteLine("登陆成功！");
                }
                else
                    Console.WriteLine("账号密码错误！");
                return user;
            }
            else
            {
                Console.WriteLine("不存在该用户");
                return null;
            }
        }

        /// <summary>
        /// 查看所有清单
        /// </summary>
        /// <param name="user"></param>
        public void ViewAllList(int userId)
        {
            List<Inventory> inventorys = new List<Inventory>();
            inventorys = _inventoryServer.GetViewAllInventoryt(userId);
            if (inventorys != null)
            {
                foreach (var inventory in inventorys)
                {
                    Console.WriteLine($"清单ID：{ inventory.Id};\t创建时间：{inventory.CreateionTime};" +
                        $"\t清单名称：{inventory.Name};\t用户ID:{inventory.UserId}");
                }
            }
            else
            {
                Console.WriteLine("无清单可查看");
            }
        }

        /// <summary>
        /// 查看所有事项
        /// </summary>
        /// <param name="user"></param>
        public void ViewAllThing(int userId)
        {
            List<Things> things = new List<Things>();
            things = _thingServer.GetViewAllThing(userId);
            if (things != null)
            {
                foreach (var thing in things)
                {
                    Console.WriteLine($"事项ID：{thing.Id};\t创建时间：{thing.CreateTime};\t" +
                        $"事项内容：{thing.ThingContent};\n事项完成状态：{thing.CompleteStatue};\t" +
                        $"过期时间：{thing.OverTime};\t备注：{thing.Remark}\t是否过期：{thing.IsPastdue}"
                        +$"\t用户ID：{thing.UserId};\t清单ID:{thing.InventoryId}");
                }
            }
            else
            {
                Console.WriteLine("无事件可查看");
            }
        }

        /// <summary>
        /// 统计未完成事件
        /// </summary>
        /// <param name="user"></param>
        public void CountOfNotCompelet(int userId)
        {
            int count = _thingServer.CountNotCompelet(userId);
            Console.WriteLine("未完成事项有:{0}", count);
        }

        /// <summary>
        /// 过期事件
        /// </summary>
        /// <param name="user"></param>
        public void OverTimeThing(int userId)
        {
            List<Things> things = new List<Things>();
            things =_thingServer.GetOverTimeThings(userId);
            if (things != null)
            {
                foreach (var thing in things)
                {
                    Console.WriteLine($"事项ID：{thing.Id};\t创建时间：{thing.CreateTime};\t" +
                        $"事项内容：{thing.ThingContent};\n事项完成状态：{thing.CompleteStatue};\t" +
                        $"过期时间：{thing.OverTime};\t备注：{thing.Remark}\t是否过期：{thing.IsPastdue}"
                        + $"\t用户ID：{thing.UserId};\t清单ID:{thing.InventoryId}");
                }
            }
            else
            {
                Console.WriteLine("无过期事项");
            }
        }

        /// <summary>
        /// 创建一个清单
        /// </summary>
        /// <param name="user"></param>
        public void CreateInventory(int userId)
        {
            string listName;
            Console.WriteLine("请输入清单的名称");
            listName = Console.ReadLine();

            _inventoryServer.CreateInventory(userId, listName);
            Console.WriteLine("创建成功");
        }
       
        /// <summary>
        /// 查看清单
        /// </summary>
        /// <param name="user"></param>  
        public void ViewInventory(int userId)
        {
            Console.WriteLine("输入要查看的清单的ID：");
            int inventoryId = InputANumber();
            Inventory inventory = _inventoryServer.GetInventory(userId, inventoryId);
            if (inventory != null)
            {
                Console.WriteLine($"清单ID：{ inventory.Id};\t创建时间：{inventory.CreateionTime};" +
                        $"\t清单名称：{inventory.Name};\t用户ID:{inventory.UserId}");
            }
            else
            {
                Console.WriteLine("该用户不存在此清单ID");
            }
        }

        /// <summary>
        /// 修改清单名称
        /// </summary>
        /// <param name="user"></param>
        public void ChangeListName(int userId)
        {
            Console.WriteLine("输入清单ID");
            int listId = InputANumber();

            if (_inventoryServer.belongTo(userId, listId))
            {
                Console.WriteLine("输入新的名称");
                string newName = Console.ReadLine();

                Inventory inventory = _inventoryServer.GetInventory(userId, listId);
                _inventoryServer.ChangeName(newName, inventory);
                _ctx.SaveChanges();
            }
            else
            {
                Console.WriteLine("该用户没有此清单ID");
            }
        }

        /// <summary>
        /// 查看清单所有事项
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public void ViewAllThingInList(int userId)
        {
            List<Things> things = new List<Things>();

            Console.WriteLine("输入清单ID");
            int inventoryId = InputANumber();

            Inventory inventory = _inventoryServer.GetInventory(userId, inventoryId);
            if (inventory != null)
            {
                things = _inventoryServer.ViewAllThingInList(inventoryId);

                if (things != null)
                {
                    foreach (var thing in things)
                    {
                        Console.WriteLine($"事项ID：{thing.Id};\t创建时间：{thing.CreateTime};\t" +
                        $"事项内容：{thing.ThingContent};\n事项完成状态：{thing.CompleteStatue};\t" +
                        $"过期时间：{thing.OverTime};\t备注：{thing.Remark}\t是否过期：{thing.IsPastdue}"
                        + $"\t用户ID：{thing.UserId};\t清单ID:{thing.InventoryId}");
                    }
                }
                else
                {
                    Console.WriteLine("没有事项可查看");
                }
            }
            else
            {
                Console.WriteLine("该用户没有此清单");
            }
        }

        /// <summary>
        /// 删除清单
        /// </summary>
        /// <param name="userId"></param>
        public void DeleteInventory(int userId)
        {
            Console.WriteLine("输入清单ID");
            int inventoryId = InputANumber();
            if (_inventoryServer.belongTo(userId, inventoryId))
            {
                _inventoryServer.RemoveInventory(inventoryId);
            }
            else
            {
                Console.WriteLine("该用户没有此清单ID");
            }
        }

        /// <summary>
        /// 添加事项
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void AddThing(int userId)
        {
            string content, remark;

            Console.WriteLine("输入清单ID");
            int inventoryId = InputANumber();
            //判断清单是否为该用户所有
            if (_inventoryServer.belongTo(userId, inventoryId))
            {
                Console.WriteLine("输入事项名称：");
                string name = Console.ReadLine();
                Console.WriteLine("输入事项内容");
                content = Console.ReadLine();
                Console.WriteLine("输入事项备注");
                remark = Console.ReadLine();
                Console.WriteLine("输入事项过期时间(YYYY-M-DD hh:mm:ss)");
                DateTime expirationDate = InputTime();
                _thingServer.AddThing(userId, inventoryId, content,
                          expirationDate, remark,name);
                Console.WriteLine("添加成功");
            }
            else
            {
                Console.WriteLine("该用户没有此清单ID");
            }
        }

        /// <summary>
        /// 删除事项
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void RemoveThing(int userId)
        {
            Console.WriteLine("输入事项ID");
            int thingId = InputANumber();
            Things thing = _thingServer.GetThing(userId,thingId);
            if (thing != null)
            {
                _thingServer.RemoveThing(thing);
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("用户不存在此事项ID");
            }
        }

        /// <summary>
        /// 修改备注
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void AddRemark(int userId)
        {
            int thingId;
            string remark;

            Console.WriteLine("输入事项ID");
            thingId = InputANumber();

            Things thing = new Things();
            thing = _thingServer.GetThing(userId, thingId);
            if (thing!=null)
            {
                Console.WriteLine("输入事项备注");
                remark = Console.ReadLine();
                _thingServer.AddRemark(thing, remark);
                Console.WriteLine("添加成功");
            }
            else
            {
                Console.WriteLine("用户不存在此事项ID");
            }
        }

        /// <summary>
        /// 修改过期时间
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void AddExpirationDate(int userId)
        {
            int thingId;
            DateTime expirationDate;

            Console.WriteLine("输入事项ID");
            thingId = InputANumber();

            Things thing = new Things();
            thing = _thingServer.GetThing(userId, thingId);
            if (thing!=null)
            {
                Console.WriteLine("输入事项过期时间(YYYY-M-DD hh:mm:ss)");
                expirationDate = InputTime();

                _thingServer.AddExpirationDate(thing, expirationDate);
                Console.WriteLine("添加成功");
            }
            else
            {
                Console.WriteLine("用户不存在此事项ID");
            }
           
        }

        /// <summary>
        /// 改变事件状态
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void ChangeStatue(int userId)
        {
            int thingId;
            bool newStatue;

            Console.WriteLine("输入事项ID");
            thingId = InputANumber();
            Things thing = new Things();
            thing = _thingServer.GetThing(userId, thingId);
            if (thing!=null)
            {
                Console.WriteLine("输入事项完成状态(true or false)");
                newStatue = Convert.ToBoolean(Console.ReadLine());

                _thingServer.ChangeState(thing, newStatue);
                Console.WriteLine("添加成功");
            }
            else
            {
                Console.WriteLine("用户不存在此事项ID");
            }
            
        }

        /// <summary>
        /// 查看事项
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventory"></param>
        public void FindThing(int userId)
        {
            int thingId;

            Console.WriteLine("输入事项ID");
            thingId = InputANumber();
            Things thing = new Things();
            thing = _thingServer.GetThing(userId, thingId);
            if (thing!=null)
            {
                Console.WriteLine($"事项ID：{thing.Id};\t创建时间：{thing.CreateTime};\t" +
                        $"事项内容：{thing.ThingContent};\n事项完成状态：{thing.CompleteStatue};\t" +
                        $"过期时间：{thing.OverTime};\t备注：{thing.Remark}\t是否过期：{thing.IsPastdue}"
                        + $"\t用户ID：{thing.UserId};\t清单ID:{thing.InventoryId}");
            }
            else
            {
                Console.WriteLine("用户不存在此事项ID");
            }

        }

        /// <summary>
        /// 判断输入是否为数字
        /// </summary>
        /// <returns></returns>
        public int InputANumber()
        {
            while (true)
            {
                try
                {
                    int result = Convert.ToInt32(Console.ReadLine());
                    return result;
                }
                catch (FormatException e)
                {
                    Console.WriteLine("请输入数字");
                }
            }
        }

        /// <summary>
        /// 判断是否输入为时间
        /// </summary>
        public DateTime InputTime()
        {
            while (true)
            {
                try
                {
                    DateTime result = Convert.ToDateTime(Console.ReadLine());
                    return result;
                }
                catch (FormatException e)
                {
                    Console.WriteLine("请输入正确格式的时间");
                }
            }
        }

    }
}
